package com.yangzc.lion.sys.security.sso;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.yangzc.lion.sys.util.Commons;

public class SSOAuthenticationProcessingFilter implements Filter {

	protected AutoLoginHelper autoLoginHelper;

	public AutoLoginHelper getAutoLoginHelper() {
		if (autoLoginHelper == null) {
			autoLoginHelper = (AutoLoginHelper) Commons.SpringUtil
					.getBean("autoLoginHelper");
		}
		return autoLoginHelper;
	}

	private static List<SSOLoginAuthentication> ssoLoginAuthentication = null;

	public void vinit() {
		if (ssoLoginAuthentication == null) {
			ssoLoginAuthentication = new ArrayList<SSOLoginAuthentication>();
			List<SSOLoginAuthentication> list = Commons.SpringUtil
					.getBeansForType(SSOLoginAuthentication.class);
			if (list != null && !list.isEmpty()) {
				ssoLoginAuthentication.addAll(list);
			}
		}
	}

	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		vinit();
		for (SSOLoginAuthentication login : ssoLoginAuthentication) {
			login.doLogin(getAutoLoginHelper(), (HttpServletRequest) request,
					(HttpServletResponse) response);
		}
		chain.doFilter(request, response);
	}

	public void destroy() {

	}

	public void init(FilterConfig filterConfig) throws ServletException {

	}
}
